package com.zhuiyun.project.api.products.product.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zhuiyun.project.api.products.product.mapper.ProductMapper;
import com.zhuiyun.project.api.products.product.service.ProductService;
import com.zhuiyun.project.api.products.product.vo.ProductSortVo;
import com.zhuiyun.project.api.products.product.entity.Product;
import com.zhuiyun.project.api.products.productsort.entity.ProductSort;
import com.zhuiyun.project.api.products.productsort.service.ProductSortService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.BeanUtils;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;

import com.zhuiyun.project.utils.pageUtil.PageResult;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName ProductServiceImpl
 * @Description TODO 商品接口实现类
 * @Author ypq
 * @Date 2023/10/25 19:12
 **/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductSortService productSortService;

    /**
    * @Author ypq
    * @Description  查询商品
    * @Date 2023/10/25 19:24
    * @Return PageResult
    * @param pageRequest
    * @param productVo
    */
    @Override
    public PageResult listProductByPages(PageRequest pageRequest, ProductSortVo productVo) {
        //获取分页参数
        MybatisPageHelper.startPage(pageRequest);
        //查询所有商品
        List<ProductSortVo> productList = productMapper.listProducts(productVo);
        //获取分页后的结果
        PageResult pageResult = MybatisPageHelper.getPageResult(productList);
        return pageResult;
    }

    /**
    * @Author ypq
    * @Description 新增商品
    * @Date 2023/10/26 16:01
    * @Return CommonResult
    * @param productSortVo
    */
    @Override
    @Transactional
    public CommonResult addProduct(ProductSortVo productSortVo) throws CommonException {
        // 验证参数的规范性
        if(ValidatorUtil.ValidationObject(productSortVo)){
            Product product = new Product();
            ProductSort productSort = new ProductSort();
            //将数据拷贝到对应类中
            BeanUtils.copy(productSortVo,product);
            BeanUtils.copy(productSortVo,productSort);

            //新增商品
            String[] mainImgIds = product.getMainImgId().split(",");
            String[] detailImgIds = product.getDetailImgId().split(",");
            if(mainImgIds.length>9){
                //如果主图数量不对，返回提示信息
                return CommonResult.error(EmErrorCode.MAINIMG_ERROR);
            }
            if(detailImgIds.length != 4){
                //如果详情图数量不对，返回提示信息
                return CommonResult.error(EmErrorCode.DETAILIMG_ERROR);
            }
            productMapper.addProduct(product);
            //获取新增后的主键id，并新增商品分类关联表
            productSort.setProductId(product.getProductId());
            //新增商品分类关联表
            boolean b = productSortService.addSortId(productSort);
            // 判断是否操作成功
            if(b){
                //新增成功，返回商品id
                Long productId = product.getProductId();
                return CommonResult.ok(productId);
            }else{
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        }else{
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }

    /**
    * @Author ypq
    * @Description 修改商品
    * @Date 2023/10/26 16:46
    * @Return CommonResult
    * @param productSortVo
    */
    @Override
    @Transactional
    public CommonResult editProduct(ProductSortVo productSortVo) throws CommonException {
        // 验证参数的规范性
        if(ValidatorUtil.ValidationObject(productSortVo)){
            Product product = new Product();
            ProductSort productSort = new ProductSort();
            //将数据拷贝到对应类中
            BeanUtils.copy(productSortVo,product);
            BeanUtils.copy(productSortVo,productSort);

            //修改商品表
            String[] mainImgIds = product.getMainImgId().split(",");
            String[] detailImgIds = product.getDetailImgId().split(",");
            if(mainImgIds.length<1 || mainImgIds.length>9){
                //如果主图数量不对，返回提示信息
                return CommonResult.error(EmErrorCode.MAINIMG_ERROR);
            }
            if(detailImgIds.length != 4){
                //如果详情图数量不对，返回提示信息
                return CommonResult.error(EmErrorCode.DETAILIMG_ERROR);
            }
            productMapper.editProduct(product);
            //修改商品分类关联表
            int i = productSortService.editSortIdByProductId(productSort);
            // 判断是否操作成功
            if(i>0){
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
            }
        }else{
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }

    /**
    * @Author ypq
    * @Description 下架商品
    * @Date 2023/10/26 19:17
    * @Return int
    * @param product
    */
    @Override
    public int downProduct(Product product) {
        //根据接受到的值传到mapper执行SQL语句
        return productMapper.editProduct(product);
    }

    /**
    * @Author ypq
    * @Description 根据商品编号获取信息
    * @Date 2023/10/26 19:33
    * @Return ProductSortVo
    * @param productId
    */
    @Override
    public ProductSortVo getProductById(Long productId) {
        //根据接受到的值传到mapper执行SQL语句
        return productMapper.getProductById(productId);
    }

    /**
     * @Author ypq
     * @Description 根据分类id查询商品
     * @Date 2023/10/25 19:34
     * @Return  List<List<ProductSortVo>>
     * @param productVo
     */
    @Override
    public  List<List<ProductSortVo>> listProducts(ProductSortVo productVo) {
        //根据分类id查询商品
        List<ProductSortVo> list = productMapper.listProducts(productVo);
        //只保留已上架商品
        List<ProductSortVo> productList = list.stream().filter(s -> ("1").equals(s.getShelfStatus())).collect(Collectors.toList());
        if (productList == null || productList.isEmpty() || productList.size() < 1) {
            return Collections.emptyList();
        }
        //如果productList不为空，将它切分成多个小集合
        List<List<ProductSortVo>> lists = Lists.partition(productList, 8);
        return lists;
    }

    /**
    * @Author ypq
    * @Description 搜索商品
    * @Date 2023/11/14 14:13
    * @Return  List<ProductSortVo>
    * @param productVo
    */
    @Override
    public List<ProductSortVo> listProductByNames(ProductSortVo productVo) {
        //根据商品名称获取商品列表
        List<ProductSortVo> products = productMapper.listProducts(productVo);
        //只保留已上架商品
        List<ProductSortVo> productList = products.stream().filter(s -> ("1").equals(s.getShelfStatus())).collect(Collectors.toList());
        return productList;
    }


}
